// pages/operation/student/studentIndex/index.js
import { StudentModel } from "../../../../api/api-work/student.js"
import { ClueModel } from "../../../../api/api-work/clueModel.js"
import { throttle } from "../../../../utils/index.js"
const studentModel = new StudentModel()
const clueModel = new ClueModel()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeTab: "first",
    firstWxParam: "",
    firstPageNum: 1,
    firstTotal: 0,
    firstTableDatas: [],
    firstTriggered: false,
    firstLoadStatus: "",
    secondWxParam: "",
    secondPageNum: 1,
    secondTotal: 0,
    secondTableDatas: [],
    secondTriggered: false,
    secondLoadStatus: "",
    remainderLessonTotal: "",
    remainderAmountTotal: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    await this.getStudentDatas()
    await this.getStudiedDatas()
    await this.getCount()
  },

  // tab切换
  onTabChange(e) {
    if (e.detail.name == "first") {
      // 在读学员
    }
    else {
      // 报读学员
    }
  },
  searchStudent: throttle(function (e) {
    this.setData({
      firstPageNum: 1,
      firstTableDatas: []
    })
    this.getStudentDatas()
  }, 500),
  // 获取在读学员列表
  async getStudentDatas() {
    clueModel.getClueListApi({ wxParam: this.data.firstWxParam, pageNum: this.data.firstPageNum, pageSize: 15, stage: 2, type: 4 }).then(res => {
      let rows = this.data.firstTableDatas;
      rows.push(...res.rows)
      this.setData({
        firstTableDatas: rows,
        firstTotal: res.total
      })
      if (this.data.firstTotal == 0) {
        this.setData({
          firstLoadStatus: ""
        })
      }
      else if (this.data.firstTableDatas.length < this.data.firstTotal) {
        this.setData({
          firstLoadStatus: "loadmore"
        })
      }
      else {
        this.setData({
          firstLoadStatus: "nomore"
        })
      }
    })
  },
  // 在读学员上拉加载
  getMoreStudying() {
    if (this.data.firstLoadStatus === "nomore") {
      return
    }
    this.setData({
      firstPageNum: this.data.firstPageNum + 1,
      firstLoadStatus: "loading"
    })
    this.getStudentDatas();
  },
  // 在读学员下拉刷新
  refreshMoreStudying() {
    this.setData({
      firstTriggered: false,
      firstPageNum: 1,
      firstTableDatas: []
    })
    this.getStudentDatas();
  },
  searchStudent2: throttle(function (e) {
    this.setData({
      secondPageNum: 1,
      secondTableDatas: []
    })
    this.getStudiedDatas()
  }, 500),
  // 
  async getCount() {
    studentModel.getRemainderCountApi().then(res => {
      this.setData({
        remainderLessonTotal: res.remainderLessonTotal,
        remainderAmountTotal: res.remainderAmountTotal
      })
    })
  },
  // 获取报读学员
  async getStudiedDatas() {
    studentModel.getEnrollmentStatusApi({ wxParam: this.data.secondWxParam, pageNum: this.data.secondPageNum, pageSize: 15, stage: 4 }).then(res => {

      let rows = this.data.secondTableDatas;
      rows.push(...res.rows)
      this.setData({
        secondTableDatas: rows,
        secondTotal: res.total
      })
      if (this.data.secondTotal == 0) {
        this.setData({
          secondLoadStatus: ""
        })
      }
      else if (this.data.secondTableDatas.length < this.data.secondTotal) {
        this.setData({
          secondLoadStatus: "loadmore"
        })
      }
      else {
        this.setData({
          secondLoadStatus: "nomore"
        })
      }
    })
  },
  getMoreStudied() {
    if (this.data.secondLoadStatus === "nomore") {
      return
    }
    this.setData({
      secondPageNum: this.data.secondPageNum + 1,
      secondLoadStatus: "loading"
    })
    this.getStudiedDatas();
  },
  refreshMoreStudied() {
    this.setData({
      secondTriggered: false,
      secondPageNum: 1,
      secondTableDatas: []
    })
    this.getStudiedDatas();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  // 点击学员
  selectStudent(e) {
    const obj = {
      tabIndex: 1, // 跳转到哪个tab页
      ...e.currentTarget.dataset.query
    }
    const data = JSON.stringify(obj);
    wx.navigateTo({
      url: `/pages/operation/clueManage/clueDetail/index?obj=${data}`,
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})